home *** CD-ROM | disk | FTP | other *** search
/ Let's Discover Texas! / Let's Discover Texas!.iso / pc / FILES / FUND.dxr / 00076_Random Movement and Rotation.ls < prev    next >
Encoding:
Text File  |  2001-01-04  |  7.3 KB  |  205 lines

  1. property pSprite, pCenterOffset, pLimits, pLimitsOrigin, pPath, pMovePeriod, pMoveStart, pMoveEnd, pRotate, pRotateStart, pRotatePeriod, pRotateEnd, pLimitsLeft, pLimitsRight, pLimitsTop, pLimitsBottom, pSpeed, pRotationSpeed, pLoopiness, pWackiness
  2.  
  3. on beginSprite me
  4.   mInitialize(me)
  5. end
  6.  
  7. on prepareFrame me
  8.   mUpdate(me)
  9. end
  10.  
  11. on mInitialize me
  12.   pSprite = sprite(me.spriteNum)
  13.   vMember = pSprite.member
  14.   if pRotationSpeed > 0 then
  15.     case vMember.type of
  16.       #field, #picture:
  17.         pRotationSpeed = 0
  18.         Message = substituteStrings(me, "Sprite ^0: ^1 behavior cannot rotate #field or #picture sprites. " & "Set the Rotation Speed parameter for this sprite to 0 to prevent this dialog from appearing.", ["^0": pSprite.spriteNum, "^1": "Random Movement and Rotation"])
  19.         alert(Message)
  20.     end case
  21.   end if
  22.   vRect = pSprite.rect
  23.   vHalfHeight = vRect.height / 2
  24.   vHalfWidth = vRect.width / 2
  25.   vMaxDimension = max(vHalfHeight, vHalfWidth)
  26.   vFarCorner = max(mVectorLength(pSprite.loc - point(vRect.left, vRect.top)), mVectorLength(pSprite.loc - point(vRect.right, vRect.top)), mVectorLength(pSprite.loc - point(vRect.left, vRect.bottom)), mVectorLength(pSprite.loc - point(vRect.right, vRect.bottom)))
  27.   vCenter = point(vHalfWidth, vHalfHeight) + point(vRect.left, vRect.top)
  28.   pCenterOffset = vCenter - pSprite.loc
  29.   pLimits = rect(pLimitsLeft, pLimitsTop, pLimitsRight, pLimitsBottom)
  30.   pLimits = pLimits + rect(vFarCorner, vFarCorner, -vFarCorner, -vFarCorner)
  31.   if (pLimits.width < vRect.width) or (pLimits.height < vRect.height) then
  32.     Message = substituteStrings(me, "Sprite ^0: ^1 behavior movement limitations are too small to allow for full rotation of sprite.", ["^0": pSprite.spriteNum, "^1": "Random Movement and Rotation"])
  33.     alert(Message)
  34.   end if
  35.   pLimitsOrigin = point(pLimits.left, pLimits.top)
  36.   mNewPath(me)
  37.   mNewRotation(me)
  38. end
  39.  
  40. on substituteStrings me, parentString, childStringList
  41.   i = childStringList.count()
  42.   repeat while i
  43.     tempString = EMPTY
  44.     dummyString = childStringList.getPropAt(i)
  45.     replacement = childStringList[i]
  46.     lengthAdjust = dummyString.char.count - 1
  47.     repeat while 1
  48.       position = offset(dummyString, parentString)
  49.       if not position then
  50.         parentString = tempString & parentString
  51.         exit repeat
  52.         next repeat
  53.       end if
  54.       if position <> 1 then
  55.         tempString = tempString & parentString.char[1..position - 1]
  56.       end if
  57.       tempString = tempString & replacement
  58.       delete me.char[1..position + lengthAdjust]
  59.     end repeat
  60.     i = i - 1
  61.   end repeat
  62.   return parentString
  63. end
  64.  
  65. on mUpdate me
  66.   vTime = the milliSeconds
  67.   mMove(me, vTime)
  68.   mRotate(me, vTime)
  69. end
  70.  
  71. on mMove me, vTime
  72.   if pSpeed then
  73.     if vTime < pMoveEnd then
  74.       vElapsed = vTime - pMoveStart
  75.       if vElapsed > 0 then
  76.         vT1 = float(vElapsed) / pMovePeriod
  77.         vT2 = vT1 * vT1
  78.         vT3 = vT2 * vT1
  79.         vNewPosition = pPath.p0
  80.         vModPoint = pPath.dc * vT1
  81.         vNewPosition = vNewPosition + vModPoint
  82.         vModPoint = pPath.db * vT2
  83.         vNewPosition = vNewPosition + vModPoint
  84.         vModPoint = pPath.da * vT3
  85.         vNewPosition = vNewPosition + vModPoint
  86.         pSprite.loc = vNewPosition
  87.       end if
  88.     else
  89.       pSprite.loc = pPath.p3
  90.       mNewPath(me)
  91.     end if
  92.   end if
  93. end
  94.  
  95. on mRotate me, vTime
  96.   if pRotationSpeed then
  97.     if vTime < pRotateEnd then
  98.       vElapsed = vTime - pRotateStart
  99.       if vElapsed > 0 then
  100.         vRotation = pRotate.start + (pRotate.diff * vElapsed / pRotatePeriod)
  101.         pSprite.rotation = vRotation
  102.       end if
  103.     else
  104.       pSprite.rotation = pRotate.end
  105.       mNewRotation(me)
  106.     end if
  107.   end if
  108. end
  109.  
  110. on mNewPath me
  111.   if voidp(pPath) then
  112.     pPath = [#p0: pSprite.loc, #p1: pSprite.loc, #p2: pSprite.loc, #p3: pSprite.loc]
  113.   end if
  114.   if pSpeed then
  115.     vDest = point(random(pLimits.width), random(pLimits.height)) + pLimitsOrigin
  116.     vP0 = pPath.p3
  117.     vVector = vDest - vP0
  118.     vVectorLen = mVectorLength(vVector)
  119.     vLoopiness = vVectorLen * pLoopiness / 25
  120.     vP1 = mRestrain(vP0 + (pPath.p3 - pPath.p2), pLimits)
  121.     if vLoopiness then
  122.       vRandomPoint = point(mRandomSign() * random(vLoopiness), mRandomSign() * random(vLoopiness))
  123.     else
  124.       vRandomPoint = point(0, 0)
  125.     end if
  126.     vP2 = mRestrain(vP0 + (vVector * 2 / 3) + vRandomPoint, pLimits)
  127.     pPath = [#p0: vP0, #p1: vP1, #p2: vP2, #p3: vDest]
  128.     setaProp(pPath, #dc, 3 * (pPath.p1 - pPath.p0))
  129.     setaProp(pPath, #db, (3 * (pPath.p2 - pPath.p1)) - pPath.dc)
  130.     setaProp(pPath, #da, pPath.p3 - pPath.p0 - pPath.dc - pPath.db)
  131.     vDistance = mVectorLength(pPath.p0 - pPath.p1) + mVectorLength(pPath.p1 - pPath.p2) + mVectorLength(pPath.p2 - pPath.p3)
  132.     pMovePeriod = vDistance * 1000 / pSpeed
  133.     pMoveStart = the milliSeconds
  134.     pMoveEnd = pMoveStart + pMovePeriod
  135.   end if
  136. end
  137.  
  138. on mNewRotation me
  139.   if voidp(pRotate) then
  140.     pRotate = [#start: 0, #end: 0, #diff: 0]
  141.   end if
  142.   if pRotationSpeed then
  143.     vRotation = pSprite.rotation
  144.     if pRotate.diff < 0 then
  145.       vOffset = random(pWackiness)
  146.     else
  147.       vOffset = -random(pWackiness)
  148.     end if
  149.     vTargetRotation = vRotation + vOffset
  150.     pRotateStart = the milliSeconds
  151.     pRotatePeriod = abs(vOffset) * 1000 / pRotationSpeed * 1000 / 360
  152.     pRotateEnd = pRotateStart + pRotatePeriod
  153.     pRotate = [#start: vRotation, #end: vTargetRotation, #diff: vOffset]
  154.   end if
  155. end
  156.  
  157. on mVectorLength vVector
  158.   vSquare = (vVector.locH * vVector.locH) + (vVector.locV * vVector.locV)
  159.   return sqrt(vSquare)
  160. end
  161.  
  162. on mRandomSign
  163.   return (random(2) * 2) - 3
  164. end
  165.  
  166. on mRestrain vPoint, vRect
  167.   vPoint.locH = max(vRect.left, min(vRect.right, vPoint.locH))
  168.   vPoint.locV = max(vRect.top, min(vRect.bottom, vPoint.locV))
  169.   return vPoint
  170. end
  171.  
  172. on isOKToAttach me, aSpriteType, aSpriteNum
  173.   case aSpriteType of
  174.     #graphic:
  175.       return getPos([#animGif, #bitmap, #field, #flash, #picture, #text, #vectorShape], sprite(aSpriteNum).member.type) <> 0
  176.     #script:
  177.       return 0
  178.   end case
  179. end
  180.  
  181. on getPropertyDescriptionList me
  182.   if not (the currentSpriteNum) then
  183.     exit
  184.   end if
  185.   vRect = (the stage).sourceRect
  186.   vRect = offset(vRect, -vRect.left, -vRect.top)
  187.   vMemberType = sprite(the currentSpriteNum).member.type
  188.   case vMemberType of
  189.     #text, #picture:
  190.       vRotateSpeed = 0
  191.     otherwise:
  192.       vRotateSpeed = 100
  193.   end case
  194.   vPDList = [:]
  195.   setaProp(vPDList, #pLimitsLeft, [#comment: "Limit of movement (left)", #format: #integer, #default: 0, #range: [#min: 0, #max: vRect.width]])
  196.   setaProp(vPDList, #pLimitsTop, [#comment: "Limit of movement (top)", #format: #integer, #default: 0, #range: [#min: 0, #max: vRect.height]])
  197.   setaProp(vPDList, #pLimitsRight, [#comment: "Limit of movement (right)", #format: #integer, #default: vRect.width, #range: [#min: 0, #max: vRect.width]])
  198.   setaProp(vPDList, #pLimitsBottom, [#comment: "Limit of movement (bottom)", #format: #integer, #default: vRect.height, #range: [#min: 0, #max: (the stage).rect.height]])
  199.   setaProp(vPDList, #pSpeed, [#comment: "Speed of movement", #format: #integer, #default: 100, #range: [#min: 0, #max: 1000]])
  200.   setaProp(vPDList, #pLoopiness, [#comment: "Loopiness", #format: #integer, #default: 10, #range: [#min: 0, #max: 25]])
  201.   setaProp(vPDList, #pRotationSpeed, [#comment: "Speed of rotation", #format: #integer, #default: vRotateSpeed, #range: [#min: 0, #max: 1000]])
  202.   setaProp(vPDList, #pWackiness, [#comment: "Wackiness", #format: #integer, #default: 120, #range: [#min: 0, #max: 360]])
  203.   return vPDList
  204. end
  205.